package com.project2.controller;


import com.project2.util.ValidationUtil;
import com.project2.anno.SaveLog;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.ResultVo;
import com.project2.bean.po.MarketAdmin;
import com.project2.service.AdminAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author : bunnyupup
 * @description :
 * @date : Created in 2022/8/12 16:02
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    AdminAdminService adminAdminService;

    /**
     * 显示所有系统管理员信息或根据姓名模糊查询
     *
     * @param username
     * @param info
     * @return
     */
    @RequiresPermissions("admin:admin:list")
    @RequestMapping("list")
    public ResultVo list(String username, PageBo info) {
        CommonData<MarketAdmin> list = adminAdminService.list(username, info);
        return ResultVo.ok(list);
    }

    /**
     * 新增管理员
     *
     * @param
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @SaveLog(value = "添加管理员")
    @RequestMapping("create")
//    public ResultVo create(@RequestBody AdminCreateVo adminCreateVo){
//        adminAdminService.create(adminCreateVo);
//        return ResultVo.ok();
//    }
    public ResultVo create(@RequestBody @Valid MarketAdmin marketAdmin, BindingResult bindingResult) throws Exception {
        ValidationUtil.valid2(bindingResult);
        int code = adminAdminService.create(marketAdmin);
        if (code == 0) {
            return ResultVo.wrong("管理员重名，请核实！");
        }
        return ResultVo.ok(marketAdmin);
    }

    /**
     * 更新管理员信息
     *
     * @param marketAdmin
     * @return
     */
    @RequiresPermissions("admin:admin:update")
    @SaveLog(value = "编辑管理员")
    @RequestMapping("update")
//    public ResultVo update(@RequestBody MarketAdmin marketAdmin) throws Exception {
    public ResultVo update(@RequestBody @Valid MarketAdmin marketAdmin, BindingResult bindingResult) throws Exception {
        ValidationUtil.valid2(bindingResult);
        int code = adminAdminService.update(marketAdmin);
        if (code == 0) {
            return ResultVo.wrong("管理员重名，请核实！");
        }
        return ResultVo.ok(marketAdmin);
    }

    /**
     * 删除管理员
     *
     * @param marketAdmin
     * @return
     */
    @RequiresPermissions("admin:admin:delete")
    @SaveLog(value = "删除管理员")
    @RequestMapping("delete")
    public ResultVo delete(@RequestBody MarketAdmin marketAdmin) {
        // 只有超管能进入这个界面，超管不能删除自身正在登录的id信息
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()){
            MarketAdmin admin = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
            if(admin.getUsername().equals(marketAdmin.getUsername())){
                return ResultVo.wrong("无法删除自身账户");
            }
        }
        adminAdminService.delete(marketAdmin);
        return ResultVo.ok();
    }

}
